<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
    let obj = {
        name: "haha",
        age: 18,
        sayHello: function () {
            console.log("sayHello");
            //如果方法要使用属性或其它方法，必须通过this关键来使用
            // console.log("sayHello,name:" + name);错误，无法访问到name属性
            console.log("sayHello,name:" + this.name);//默认指向是对象本身
        },
        t1() {
            this.sayHello();
        }
    };

    let obj1 = new Object({
        name: "haha",
        age: 18
    });

    console.log(obj === obj1);
    console.log(obj == obj1);

    //点语法
    obj.study = "JavaScript";
    //中括号语法，允许通过变量来获取
    console.log(obj["study"]);
    let studyProp = "study";
    console.log(obj[studyProp]);

    obj.sayHello();
    obj.t1();

    let student = {
        name: "张三",
        age: 20,
        address: "湛江岭南师范",
        info() {
            console.log(`${this.name}`);
        }
    }
    student.info();

    // for (let i = 0; i < obj.length; i++)计数for循环
    for (let key in student) {
        console.log(key, student[key]);
    }

    //因为student不是一个迭代器对象，所以不支持for-of循环
    // for (let item of student) {
    //     console.log(item);
    // }

    //Object.keys(student)：获取所有属性名的迭代器
    for (let key of Object.keys(student)) {
        console.log(key);
    }
    //Object.values(student)：获取所有属性值的迭代器
    for (let key of Object.values(student)) {
        console.log(key);
    }
    //Object.entries(student)：获取所有属性名:属性值的键值对集合，类似于Map集合
    for (let [key, value]/*解构*/ of Object.entries(student)) {
        console.log(key, value);
    }
    for (let item of Object.entries(student)) {
        console.log(item[0]);
        console.log(item[1]);
    }

    var myObject = {a: 11, b: 22, c: 33};

    //写法一：简单写法
    myObject[Symbol.iterator] = function () {
        const _this = this
//也可使用: keys = Object.getOwnPropertyNames(this)
        const keys = Object.keys(this)
        let index = 0
        return {
            next() {
                return {
                    value: _this[keys[index++]],
                    done: index > keys.length
                }
            }
        }
    }

    for (let item of myObject) {
        console.log(item);
    }

    // new Map().keys()

    let liuyan = {
        id: 1,
        name: "柳岩",
        gender: "女"
    }
</script>
</body>
</html>